<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="统计月份" prop="date">
        <el-date-picker
          v-model="queryParams.date"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="monthrange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <!-- <el-form-item label="营业状态" prop="businessStatus">
        <el-select
          v-model="queryParams.businessStatus"
          placeholder="请选择营业状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.CRM_BUSINESS_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="行业类别" prop="industryCategory">
        <el-select
          v-model="queryParams.industryCategory"
          placeholder="请选择行业类别"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.CRM_INDUSTRY_CATEGORY)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['crm:operation:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap class="mt-10px">
    <el-table
      ref="contactRef"
      v-loading="loading"
      :data="list"
      :show-overflow-tooltip="true"
      :stripe="true"
    >
      <el-table-column
        label="统计月份"
        align="center"
        prop="date"
        :formatter="dateFormatter3"
        width="100px"
      />
      <el-table-column align="center" label="客户" prop="customerName" width="300px">
        <template #default="scope">
          <el-link :underline="false" type="primary">
            {{ scope.row.customerName }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="运营经理" align="center" prop="operationManagerName" />
      <!-- <el-table-column label="营业状态" align="center" prop="businessStatus" width="120px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CRM_BUSINESS_STATUS" :value="scope.row.businessStatus" />
        </template>
      </el-table-column>
      <el-table-column label="细分行业" align="center" prop="industryCategory" width="180px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CRM_INDUSTRY_CATEGORY" :value="scope.row.industryCategory" />
        </template>
      </el-table-column> -->
      <el-table-column
        label="设备抵押情况"
        align="center"
        prop="mortgageEquipmentSituation"
        width="120px"
      >
        <template #default="scope">
          <dict-tag
            :type="DICT_TYPE.CRM_MORTGAGE_EQUIPMENT_SITUATION"
            :value="scope.row.mortgageEquipmentSituation"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="设备抵押估值（万元）"
        align="center"
        prop="equipmentMortgageValuation"
        width="110px"
      />
      <el-table-column
        label="个人担保情况"
        align="center"
        prop="personalGuaranteeSituation"
        width="110px"
      >
        <template #default="scope">
          <dict-tag
            :type="DICT_TYPE.CRM_PERSONAL_GUARANTEE_SITUATION"
            :value="scope.row.personalGuaranteeSituation"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="其他经营概况描述"
        align="center"
        prop="otherSituations"
        width="400px"
      />
      <el-table-column label="租金配套欠费（万元）" align="center" prop="rent" />
      <el-table-column label="供应链欠费（万元）" align="center" prop="supplyChain" />
      <el-table-column label="园区总欠费（万元）" align="center" prop="allFee" />
      <el-table-column label="欠薪金额（万元）" align="center" prop="backPayAmount" />
      <el-table-column label="银行贷款（万元）" align="center" prop="loan" />
      <el-table-column label="其它社会债务（万元）" align="center" prop="otherDebts" />
      <el-table-column label="总负债估算（万元）" align="center" prop="allDebt" />
      <el-table-column label="产值（万元）" align="center" prop="outputAmount" />
      <el-table-column label="主要客户货款回笼周期（月）" align="center" prop="paymentCycle" />
      <el-table-column label="现有员工人数" align="center" prop="totalEmp" />
      <el-table-column label="缺工人数" align="center" prop="shortageEmp" />
      <el-table-column label="是否有集体裁员计划" align="center" prop="isLayoffPlan">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.isLayoffPlan" />
        </template>
      </el-table-column>
      <el-table-column label="计划裁员人数" align="center" prop="layoffs" />
      <el-table-column label="是否有降薪计划" align="center" prop="isSalaryReductionPlan">
        <template #default="scope">
          <dict-tag
            :type="DICT_TYPE.INFRA_BOOLEAN_STRING"
            :value="scope.row.isSalaryReductionPlan"
          />
        </template>
      </el-table-column>
      <el-table-column label="计划降薪幅度（%）" align="center" prop="salaryReductionRange" />
      <el-table-column label="是否欠薪" align="center" prop="isBackPay">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.isBackPay" />
        </template>
      </el-table-column>
      <el-table-column label="欠薪月数" align="center" prop="backPayDuration" />
      <el-table-column label="欠薪人数" align="center" prop="backPay" />
      <el-table-column label="出口订单占比（%）" align="center" prop="exportOrderRate" />
      <el-table-column label="内销订单占比（%）" align="center" prop="domesticOrderRate" />
      <el-table-column label="产能利用率（%）" align="center" prop="capacityRate" />
      <el-table-column
        label="当月订单饱和度（%）"
        align="center"
        prop="monthlyOrderSaturationRate"
      />
      <el-table-column label="开工天数" align="center" prop="workingDays" />
      <el-table-column label="开工时数" align="center" prop="workingHours" />
      <el-table-column label="操作" align="center" fixed="right" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['crm:operation:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['crm:operation:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      v-model:limit="queryParams.pageSize"
      v-model:page="queryParams.pageNo"
      :total="total"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加 -->
  <OperationForm ref="formRef" @success="getList" />
</template>
<script lang="ts" setup>
import { OperationApi } from '@/api/crm/operation'
import OperationForm from './../OperationForm.vue'
import { dateFormatter3 } from '@/utils/formatTime'
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { BizTypeEnum } from '@/api/crm/permission'

defineOptions({ name: 'OperationList' })
const props = defineProps<{
  bizType: number // 业务类型
  bizId: number // 业务编号
  customerId?: number // 特殊：客户编号
}>()

const { t } = useI18n() // 国际化
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  customerId: undefined,
  date: [],
  operationManager: undefined,
  businessStatus: undefined,
  industryCategory: undefined,
  otherSituations: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const message = useMessage()

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    // 置空参数
    queryParams.customerId = undefined
    // 执行查询
    let data = { list: [], total: 0 }
    switch (props.bizType) {
      case BizTypeEnum.CRM_CUSTOMER:
        queryParams.customerId = props.bizId
        data = await OperationApi.getOperationPage(queryParams)
        break
      default:
        return
    }
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id, props.bizId)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await OperationApi.deleteOperation(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 监听打开的 bizId + bizType，从而加载最新的列表 */
watch(
  () => [props.bizId, props.bizType],
  () => {
    handleQuery()
  },
  { immediate: true, deep: true }
)
</script>
